﻿namespace ChessToys.QueensProblem
{
    using System;
    using System.Collections.Generic;
    using ChessToys;

    /// <summary>
    /// Abstract implementation of <see cref="IQueensProblemSolver"/> providing basic functionality.
    /// Those funtionality is handling unsolvable n's and providing the trivial solution for n == 1.
    /// </summary>
    public abstract class AbstractQueensProblemSolver : IQueensProblemSolver
    {
        /// <inheritdoc />
        public ISet<Board> Solve(int n)
        {
            // unsolvable cases
            if (n != 1 && n < 4)
            {
                throw new ArgumentException(String.Format("The Queens Problem is unsolvable for n = {0}.", n));
            }
            // trivial case
            if (n == 1)
            {
                Board board = new Board(1);
                board[new Position(0,0)] = new Queen(Color.White);
                ISet<Board> trivialResult = new HashSet<Board>();
                trivialResult.Add(board);
                return trivialResult;
            }
            // untrivial cases
            return SolveUntrivial(n);
        }

        /// <summary>
        /// Solves the problem for untrivial n (n >=4).
        /// </summary>
        /// <param name="n">The n.</param>
        /// <returns>All solutions for the queens problem size n.</returns>
        protected abstract ISet<Board> SolveUntrivial(int n);
    }
}
